﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DAO;
using DTO;


namespace BUS
{
    public class SearchEngine
    {
        #region
        private DichVuDAO _dichVuDAO = null;
        private DuLieuDAO _duLieuDAO = null;
        private DuongDAO _duongDAO = null;
        private PhuongDAO _phuongDAO = null;
        private QuanHuyenDAO _quanHuyenDAO = null;
        private TenDiaDiemDAO _tenDiaDiemDAO = null;
        private TinhThanhDAO _tinhThanhDAO = null;
        private TuKhoaDiaDiemDAO _tuKhoaDiaDiemDAO = null;
        private TuKhoaDichVuDAO _tuKhoaDichVuDAO = null;
        private TuKhoaDuongDAO _tuKhoaDuongDAO = null;
        private TuKhoaPhuongDAO _tuKhoaPhuongDAO = null;
        private TuKhoaQuanHuyenDAO _tuKhoaQuanHuyenDAO = null;
        private TuKhoaTinhThanhDAO _tuKhoaTinhThanhDAO = null;
        private ChiTietDuLieuDAO _chitietDuLieuDAO = null;
        private ChiTietDichVuDAO _chitietDichVuDAO = null;

        private List<SearchResultObject> _lstSearchResult = null;

        private List<DichVuDTO> _lstDichVuDTO = null;
        private List<DuongDTO> _lstDuongDTO = null;
        private List<QuanHuyenDTO> _lstQuanHuyenDTO = null;
        #endregion

        #region Methol
        public SearchEngine()
        {
            try
            {
                this._dichVuDAO = new DichVuDAO();
                this._duLieuDAO = new DuLieuDAO();
                this._duongDAO = new DuongDAO();
                this._phuongDAO = new PhuongDAO();
                this._quanHuyenDAO = new QuanHuyenDAO();
                this._tenDiaDiemDAO = new TenDiaDiemDAO();
                this._tinhThanhDAO = new TinhThanhDAO();
                this._tuKhoaDiaDiemDAO = new TuKhoaDiaDiemDAO();
                this._tuKhoaDichVuDAO = new TuKhoaDichVuDAO();
                this._tuKhoaDuongDAO = new TuKhoaDuongDAO();
                this._tuKhoaPhuongDAO = new TuKhoaPhuongDAO();
                this._tuKhoaQuanHuyenDAO = new TuKhoaQuanHuyenDAO();
                this._tuKhoaTinhThanhDAO = new TuKhoaTinhThanhDAO();
                this._chitietDichVuDAO = new ChiTietDichVuDAO();
                this._chitietDuLieuDAO = new ChiTietDuLieuDAO();

                this._lstSearchResult = new List<SearchResultObject>();
                //_lstDichVuDTO = this.GetAllDichVuDTO();
                //_lstDuongDTO = this.GetAllDuongDTO();
                //_lstQuanHuyenDTO = this.GetAllQuanHuyenDTO();
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        private List<DichVuDTO> GetAllDichVuDTO()
        {
            List<DichVuDTO> lstDichVuDTO = null;
            try
            {
                lstDichVuDTO = DichVuDAO.GetAllDichVuDTO();
            }
            catch (Exception ex)
            {
                lstDichVuDTO = new List<DichVuDTO>();
                throw ex;
            }
            return lstDichVuDTO;
        }

        private  List<DuongDTO> GetAllDuongDTO()
        {
            List<DuongDTO> lstDuongDTO = null;
            try
            {
                lstDuongDTO = DuongDAO.GetAllDuongDTO();
            }
            catch (Exception ex)
            {
                lstDuongDTO = new List<DuongDTO>();
                throw ex;
            }
            return lstDuongDTO;
        }

        private  List<QuanHuyenDTO> GetAllQuanHuyenDTO()
        {
            return null;
        }

        public List<DuLieuDTO> Search(string strInput, int maDichVu)
        {
            List<DuLieuDTO> lstResult = new List<DuLieuDTO>();

            try
            {
                // xoa ket qua cu
                if (null != this._lstSearchResult)
                {
                    this._lstSearchResult.Clear();
                }
                else
                {
                    this._lstSearchResult = new List<SearchResultObject>();
                }

                

                // tim kiem
                
                strInput = Utilities.FormatInputData(strInput);
                if (strInput.Length > 0)
                {
                    this.SearchTuKhoaDichVu(strInput,maDichVu);

                    List<DuLieuDTO> lstTemp = null;
                    int nSearchResult = this._lstSearchResult.Count;
                    for (int i = 0; i < nSearchResult; i++)
                    {
                        if(this._lstSearchResult[i].MaDuong != 0)
                        {
                            lstTemp = this._duLieuDAO.Search_by_maDuong(this._lstSearchResult[i].MaDichVu,
                                                                        this._lstSearchResult[i].MaDuong);
                        }

                        if(this._lstSearchResult[i].MaQuanHuyen != 0)
                        {
                            lstTemp = this._duLieuDAO.Search_by_maQuanHuyen(this._lstSearchResult[i].MaDichVu,
                                                                        this._lstSearchResult[i].MaQuanHuyen);
                        }
                        if (lstTemp != null)
                        {
                            lstResult.AddRange(lstTemp);
                        }
                        
                    }
                }
                else
                {
                    
                    if (0 != maDichVu)
                    {
                        this.SearchTuKhoaDichVu(strInput, maDichVu);
                        List<DuLieuDTO> lstTemp = null;
                        int nSearchResult = this._lstSearchResult.Count;
                        for (int i = 0; i < nSearchResult; i++)
                        {
                            lstTemp = this._duLieuDAO.Search(
                                this._lstSearchResult[i].SoNha,
                                this._lstSearchResult[i].MaDichVu,
                                this._lstSearchResult[i].MaTinhThanh,
                                this._lstSearchResult[i].MaQuanHuyen,
                                this._lstSearchResult[i].MaPhuong,
                                this._lstSearchResult[i].MaDuong,
                                this._lstSearchResult[i].MaTenDiaDiem
                                );
                            lstResult.AddRange(lstTemp);
                        }
                    }
                     
                }
            }
            catch (System.Exception ex)
            {
                lstResult = new List<DuLieuDTO>();
                throw ex;
            }
            

            return lstResult;
        }

        // ham tra ve danh sach cac chi tiet dich vu
        public List<ChiTietDichVuDTO> SearchChiTietDichVuDTO(int maDuLieu)
        {
            List<ChiTietDichVuDTO> _lstChiTietDichVuDTO = null;
            List<ChiTietDuLieuDTO> _lstChiTietDuLieuDTO = null;

            _lstChiTietDuLieuDTO = _chitietDuLieuDAO.search(maDuLieu);
            int nlstChiTietDuLieuDTO = _lstChiTietDuLieuDTO.Count;

            for (int i = 0; i < nlstChiTietDuLieuDTO; i++)
            {
                ChiTietDichVuDTO ChiTietDichVuDTOTemp = new ChiTietDichVuDTO();
                ChiTietDichVuDTOTemp = _chitietDichVuDAO.Search(_lstChiTietDuLieuDTO[i].MaChiTiet);
                _lstChiTietDichVuDTO.Add(ChiTietDichVuDTOTemp);
            }

            return _lstChiTietDichVuDTO;
        }

        // ket bang
        public List<DuLieuThoDTO> Reference(List<DuLieuDTO> lstDuLieuDTO)
        {
            List<DuLieuThoDTO> lstDuLieuThoDTO = new List<DuLieuThoDTO>();
            try
            {
                DuLieuThoDTO duLieuThoDTO = null;
                TenDiaDiemDTO tenDiaDiemDTO = null;
                DuongDTO duongDTO = null;
                PhuongDTO phuongDTO = null;
                QuanHuyenDTO quanHuyenDTO = null;
                TinhThanhDTO tinhThanhDTO = null;
                int nlstDulieuDTO = lstDuLieuDTO.Count;

                for (int i = 0; i < nlstDulieuDTO; i++)
                {
                    duLieuThoDTO = new DuLieuThoDTO();
                    // sư dụng mã dữ liệu khi cần lấy thông tin chi chiet ve dich vu
                    duLieuThoDTO.MaDuLieu = lstDuLieuDTO[i].MaDuLieu;
                    DichVuDTO dichVuDTO = this._dichVuDAO.Search(lstDuLieuDTO[i].MaDichVu);
                    if (null != dichVuDTO)
                    {
                        duLieuThoDTO.TenDichVu = dichVuDTO.TenDichVu;
                    }

                    tenDiaDiemDTO = this._tenDiaDiemDAO.Search(lstDuLieuDTO[i].MaTenDiaDiem);
                    if (null != tenDiaDiemDTO)
                    {
                        duLieuThoDTO.TenDiaDiem = tenDiaDiemDTO.TenDiaDiem;
                    }

                    duLieuThoDTO.SoNha = lstDuLieuDTO[i].SoNha;
                    duongDTO = this._duongDAO.Search(lstDuLieuDTO[i].MaDuong);
                    if (null != duongDTO)
                    {
                        duLieuThoDTO.TenDuong = duongDTO.TenDuong;
                    }

                    phuongDTO = this._phuongDAO.Search(lstDuLieuDTO[i].MaPhuong);
                    if (null != phuongDTO)
                    {
                        duLieuThoDTO.TenPhuong = phuongDTO.TenPhuong;
                    }

                    quanHuyenDTO = this._quanHuyenDAO.Search(lstDuLieuDTO[i].MaQuanHuyen);
                    if (null != quanHuyenDTO)
                    {
                        duLieuThoDTO.TenQuanHuyen = quanHuyenDTO.TenQuanHuyen;
                    }

                    tinhThanhDTO = this._tinhThanhDAO.Search(lstDuLieuDTO[i].MaTinhThanh);
                    if (null != tinhThanhDTO)
                    {
                        duLieuThoDTO.TenTinhThanh = tinhThanhDTO.TenTinhThanh;
                    }

                    duLieuThoDTO.KinhDo = lstDuLieuDTO[i].KinhDo;
                    duLieuThoDTO.ViDo = lstDuLieuDTO[i].ViDo;
                    duLieuThoDTO.ChuThich = lstDuLieuDTO[i].ChuThich;

                    lstDuLieuThoDTO.Add(duLieuThoDTO);
                }
            }
            catch (Exception ex)
            {
                lstDuLieuThoDTO = new List<DuLieuThoDTO>();
                throw ex;
            }
            

            return lstDuLieuThoDTO;
        }

        private void SearchTuKhoaDuong(string strInput, int maDichVu)
        {
            List<TuKhoaDuongDTO> lstTuKhoaDuongDTO = this._tuKhoaDuongDAO.Search(strInput);
            int nTuKhoaDuongDTO = lstTuKhoaDuongDTO.Count;
            if (nTuKhoaDuongDTO > 0)
            {
                for (int i = 0; i < lstTuKhoaDuongDTO.Count; i++)
                {
                    SearchResultObject sr = new SearchResultObject();
                    sr.MaDichVu = maDichVu;
                    sr.MaDuong = lstTuKhoaDuongDTO[i].MaDuong;
                    this._lstSearchResult.Add(sr);
                }
            }
            /*
            else
            {
                SearchResultObject sr = new SearchResultObject();
                sr.MaDichVu = maDichVu;
                this._lstSearchResult.Add(sr);
            }
             * */
        }

        private void SearchTuKhoaQuanHuyen(string strInput, int maDichVu)
        {
            List<TuKhoaQuanHuyenDTO> lstTuKhoaQuanHuyenDTO = this._tuKhoaQuanHuyenDAO.Search(strInput);
            int nTuKhoaQuanHuyenDTO = lstTuKhoaQuanHuyenDTO.Count;
            if (nTuKhoaQuanHuyenDTO > 0)
            {
                for (int i = 0; i < lstTuKhoaQuanHuyenDTO.Count; i++)
                {
                    SearchResultObject sr = new SearchResultObject();
                    sr.MaDichVu = maDichVu;
                    sr.MaQuanHuyen = lstTuKhoaQuanHuyenDTO[i].MaQuanHuyen;
                    this._lstSearchResult.Add(sr);
                }
            }
            /*
            else
            {
                SearchResultObject sr = new SearchResultObject();
                sr.MaDichVu = maDichVu;
                this._lstSearchResult.Add(sr);
            }
             * */
        }

        private void SearchTuKhoaDichVu(string strInput, int maDichVu)
        {
            if (0 == maDichVu) // tim tat ca dich vu
            {
                List<TuKhoaDichVuDTO> lstTuKhoaDichVuDTO = this._tuKhoaDichVuDAO.Search(strInput);
                int nTuKhoaDichVuDTO = lstTuKhoaDichVuDTO.Count;
                if (nTuKhoaDichVuDTO > 0)
                {
                    string strTemp = String.Empty;
                    for (int i = 0; i < nTuKhoaDichVuDTO; i++)
                    {
                        strTemp = strInput.Clear(lstTuKhoaDichVuDTO[i].TuKhoaDichVu);
                        if (strTemp.Length > 0)
                        {
                            this.SearchTuKhoaQuanHuyen(strTemp, lstTuKhoaDichVuDTO[i].MaDichVu);
                            this.SearchTuKhoaDuong(strTemp, lstTuKhoaDichVuDTO[i].MaDichVu);
                            if(_lstSearchResult.Count == 0)
                            {
                                SearchResultObject sr = new SearchResultObject();
                                sr.MaDichVu = lstTuKhoaDichVuDTO[i].MaDichVu;
                                this._lstSearchResult.Add(sr);
                            }
                        }
                        else
                        {
                            SearchResultObject sr = new SearchResultObject();
                            sr.MaDichVu = lstTuKhoaDichVuDTO[i].MaDichVu;
                            this._lstSearchResult.Add(sr);
                        }
                    }
                }
                
            }
            else // co ma dich vu cu the
            {
                if (0 == strInput.Length)
                {
                    SearchResultObject sr = new SearchResultObject();
                    sr.MaDichVu = maDichVu;
                    this._lstSearchResult.Add(sr);
                }
                else
                {
                    this.SearchTuKhoaQuanHuyen(strInput, maDichVu);
                    this.SearchTuKhoaDuong(strInput,maDichVu);
                }
            }   
        }

        #endregion
    }
}
